import click
from exts import db
from models.user import PermissionEnum, PermissionModel, RoleModel, UserModel
from models.post import BoardModel, PostModel
from faker import Faker
import random


def create_permission():
    for permission_name in dir(PermissionEnum):
        if permission_name.startswith('__'):
            continue
        permission = PermissionModel(name=getattr(PermissionEnum, permission_name))
        db.session.add(permission)
    db.session.commit()
    click.echo('权限添加成功')


def create_role():
    # 稽查
    inspector = RoleModel(name='稽查', desc='负责审核帖子和评论是否合法合规')
    inspector.permissions = PermissionModel.query.filter(
        PermissionModel.name.in_([PermissionEnum.POST, PermissionEnum.COMMENT])).all()

    # 运营
    operator = RoleModel(name='运营', desc='负责网站持续正常运营')
    operator.permissions = PermissionModel.query.filter(PermissionModel.name.in_(
        [PermissionEnum.POST, PermissionEnum.COMMENT, PermissionEnum.BOARD, PermissionEnum.FRONT_USER])).all()

    # 管理员
    administrator = RoleModel(name='管理员', desc='负责网站管理')
    administrator.permissions = PermissionModel.query.all()
    db.session.add_all([inspector, operator, administrator])
    db.session.commit()
    click.echo('角色添加成功')


def create_test_user():
    # 创建管理员
    admin_role = RoleModel.query.filter_by(name='管理员').first()
    zhangsan = UserModel(username='张三', email='zhangsan@zlkt.net', password='111111', is_staff=True, role=admin_role)

    # 创建运营
    operator_role = RoleModel.query.filter_by(name='运营').first()
    lisi = UserModel(username='lisi', email='lisi@zlkt.net', password='111111', is_staff=True, role=operator_role)

    # 创建稽查
    inspector_role = RoleModel.query.filter_by(name='稽查').first()
    wangwu = UserModel(username='wangwu', email='wangwu@zlkt.net', password='111111', is_staff=True,
                       role=inspector_role)

    db.session.add_all([zhangsan, lisi, wangwu])
    db.session.commit()
    click.echo('测试用户添加成功')


@click.option('--username', '-u', help='输入用户名')
@click.option('--email', '-e', help='输入邮箱')
@click.option('--password', '-p', help='输入密码')
def create_admin(username, email, password):
    admin_role = RoleModel.query.filter_by(name='管理员').first()
    admin_user = UserModel(username=username, email=email, password=password, is_staff=True, role=admin_role)
    db.session.add(admin_user)
    db.session.commit()
    click.echo('管理员添加成功')


@click.option('--n', '-n', type=int, help='输入用户数量')
def create_admin_batch(n):
    admin_role = RoleModel.query.filter_by(name='管理员').first()
    for x in range(n):
        username = f'admin{x + 1}'
        email = f'{username}@zlkt.net'
        password = '111111'
        admin_user = UserModel(username=username, email=email, password=password, is_staff=True, role=admin_role)
        db.session.add(admin_user)
    db.session.commit()
    click.echo(f'管理员批量添加成功,添加了{n}个管理员')


@click.option('--name', '-n', help='输入用户角色名称')
@click.option('--count', '-c', type=int, help='输入用户数量')
def create_user_batch(count, name):
    user_role = RoleModel.query.filter_by(name=name).first()
    for x in range(count):
        if name == '稽查':
            nc = 'jc'
        elif name == '运营':
            nc = 'yyg'
        elif name == '管理员':
            nc = 'gly'
        username = f'testuser_{nc}{x + 1}'
        email = f'{username}@zlkt.net'
        password = '111111'
        admin_user = UserModel(username=username, email=email, password=password, is_staff=False, role=user_role)
        db.session.add(admin_user)
    db.session.commit()
    click.echo(f'用户批量添加成功,添加了{count}个{name}用户')


def create_board():
    board_names = ['Python语法', 'web开发', '数据分析', '测试开发', '运维开发']
    for board_name in board_names:
        board = BoardModel(name=board_name)
        db.session.add(board)
    db.session.commit()
    click.echo('板块添加成功')


def create_test_post():
    fake = Faker(locale='zh_CN')
    author = UserModel.query.first()
    boards = BoardModel.query.all()
    click.echo('开始添加帖子数据')
    for x in range(98):
        title = fake.sentence()
        content = fake.paragraph(nb_sentences=10)
        board_index = random.randint(0, 4)
        board = boards[board_index]
        post = PostModel(title=title, content=content, author=author, board=board)
        db.session.add(post)
    db.session.commit()
    click.echo('帖子数据添加成功，添加了98条数据')
